package com.ssyu.examination.system.servlet;

import java.io.IOException;




import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.system.entity.RoleEntity;
import com.ssyu.examination.system.service.RoleService;

@WebServlet("/RoleServlet")
public class RoleServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public RoleServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("listRole".equals(command)) {
			listRole(request, response);
		} else if ("addRole".equals(command)) {
			addRole(request, response);
		} else if ("changeRole".equals(command)) {
			changeRole(request, response);
		} else if ("deleteRole".equals(command)) {
			deleteRole(request, response);
		} else if ("getRole".equals(command)) {
			getRole(request, response);
		} else if ("updateRole".equals(command)) {
			updateRole(request, response);
		} 
	}


	/**
	 * 修改角色信息
	 * @param request
	 * @param response
	 */
	private void updateRole(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("roleName");
		String[] menus = request.getParameterValues("roleMenuUpdate");
		StringBuilder menuIds = new StringBuilder();
		for(int i = 0;i < menus.length;i++){
			if(i == menus.length - 1){
				menuIds.append(menus[i]);
			} else {
				menuIds.append(menus[i] + ",");
			}
		}
		
		String menu = menuIds.toString();
		int row = DBUtil
				.executeUpdate(
						"UPDATE sys_role SET name=?,menu_ids=? WHERE id=?",
						name, menu, id);

		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 获取角色信息
	 * @param request
	 * @param response
	 */
	private void getRole(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));

		RoleEntity role = DBUtil.getUniqueResult(RoleEntity.class,
				"SELECT * FROM sys_role WHERE id=?", id);

		if (role != null) {
			print(response, FastjsonUtil.toJson(role));
		}
	}


	/**
	 * 删除角色信息
	 * @param request
	 * @param response
	 */
	private void deleteRole(HttpServletRequest request,
			HttpServletResponse response) {
		String ids = request.getParameter("ids");
		int row = DBUtil.executeUpdate("DELETE FROM sys_role WHERE id IN (" + ids
				+ ")");

		if (row >= 1) {
			print(response, 1);
		}
	}

	/**
	 * 改变角色状态
	 * @param request
	 * @param response
	 */
	private void changeRole(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));

		RoleEntity roleEntity = DBUtil.getUniqueResult(RoleEntity.class, "select * from sys_role where id=?", id);
		int state = roleEntity.getState();
		if (state == 1) {
			state = 2;
		} else {
			state = 1;
		}
		int row = DBUtil.executeUpdate("update sys_role set state=? where id=? ", state, id);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 新增角色
	 * @param request
	 * @param response
	 */
	private void addRole(HttpServletRequest request,
			HttpServletResponse response) {
		String name = request.getParameter("roleName");
		// String memo = request.getParameter("roleMemo");
		int operateId = App.getOperateId(request, response);
		String[] menus = request.getParameterValues("roleMenu");
		StringBuilder menuIds = new StringBuilder();
		for(int i = 0;i < menus.length;i++){
			if(i == menus.length - 1){
				menuIds.append(menus[i]);
			} else {
				menuIds.append(menus[i] + ",");
			}
		}
		
		String menu = menuIds.toString();
		int row = DBUtil.executeUpdate(
				"INSERT INTO sys_role VALUES(NULL, ?, ?, ?,NOW() , 1,NULL)",
				name,menu,operateId);

		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 列举角色信息
	 * @param request
	 * @param response
	 */
	private void listRole(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String role = request.getParameter("role");
		PageModel<RoleEntity> pageModel = RoleService.listRole(page, rows,
				role);
		print(response, FastjsonUtil.toJsonWithDateFormat(pageModel,
				"yyyy-MM-dd HH:mm:ss"));
		
		
	}

}
